//
//  InvestCell.m
//  LingQian
//
//  Created by xieyingze on 2018/1/21.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "MeCell.h"
#import "CustomButton.h"

@interface MeCell()
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UIView *buttonsView;
@property (nonatomic, strong) NSMutableArray *buttons;
@end

@implementation MeCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    static NSString *cellId = @"MeCell";
    MeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[MeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

#pragma mark - init

- (void)initData {
    self.backgroundColor = kCOLOR_tableView;
}

- (void)initSubView {
    [self addSubview:self.mainView];
}

- (void)autoLayoutSubviews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(10, 20, 10, 20));
    }];
}

#pragma mark - Method


#pragma mark - Interface


#pragma mark - Setter

- (void)setViewModel:(MeViewModel *)viewModel {
    _viewModel = viewModel;
    [self refreshUI];
}

- (void)refreshUI {
    NSDictionary *dic = [NSDictionary dictionaryFromLocalJsonFile:[UnityTool getStringValueForKey:USER_themName] fileType:@"json"];
    for (BaseButton *btn in self.buttons) {
        btn.imageName = dic[@"home"][@"buttonIcons"][btn.tag][@"image"];
        [btn setTitleColor:kCOLOR_mainFont forState:UIControlStateNormal];
    }
}

#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        _mainView.layer.cornerRadius = kUI_LayerCorner;
        [_mainView setViewShadow:kCOLOR_shadowLight offset:CGSizeMake(0, 0) radius:kUI_ShawRadius/2];
        
        [_mainView addSubview:self.buttonsView];
        [self.buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@10);
            make.left.right.equalTo(@0);
            make.height.equalTo(@60);
        }];
    }
    return _mainView;
}

- (UIView *)buttonsView {
    if (!_buttonsView) {
        _buttonsView = [UIView new];
        NSDictionary *dic = [NSDictionary dictionaryFromLocalJsonFile:[UnityTool getStringValueForKey:USER_themName] fileType:@"json"];
        CGFloat w = (kUI_WIDTH - 40)/[dic[@"me"][@"buttonIcons"] count];
        for (int i = 0; i < [dic[@"me"][@"buttonIcons"] count]; i++) {
            CustomButton *btn = [[CustomButton alloc] initWithCustomBttonType:kCustomBttonTypeNormal size:CGSizeMake(w, 60.f)];
            btn.tag = i;
            btn.imageName = dic[@"me"][@"buttonIcons"][i][@"image"];
            [btn setButtonStyle:dic[@"me"][@"buttonIcons"][i][@"title"] titleColor:KCOLOR_nornalFont titleFont:kFONT_mainFont(10.f) texrAlignment:NSTextAlignmentCenter];
            [_buttonsView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@(i * w));
                make.centerY.equalTo(@0);
                make.width.equalTo(@(w));
                make.height.equalTo(@60);
            }];
            [self.buttons addObject:btn];
        }
    }
    return _buttonsView;
}

- (NSMutableArray *)buttons {
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
@end
